package com.weilan.blog.picture.config;

import cn.hutool.core.util.StrUtil;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Configuration
public class TokenRelayRequestInterceptor implements RequestInterceptor {



    @Value(value = "${tokenHeader}")
    private String tokenHeader;

    @Override
    public void apply(RequestTemplate requestTemplate) {
// 获取该次请求得token 将token传递
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader(tokenHeader);
        log.info(token);
        if (StrUtil.isNotBlank(token)) {
            requestTemplate.header(tokenHeader, token);
        }

    }
}
